<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@include file="/web/page/head.jsp" %>
<title>Insert title here</title>
</head>
<body>
<form class="layui-form" method="post">

			<br>
			<div class="layui-collapse">
			<div class="layui-form-item">
				<label class="layui-form-label">部门编号：</label>
				<div class="layui-input-inline">
					<input type="text" name="code" class="layui-input" lay-verity="required" placeholder="编号" />
				</div>
				<label class="layui-form-label">部门名称：</label>
				<div class="layui-input-inline">
					<input type="text" name="name" class="layui-input" lay-verity="required" placeholder="名称" />
				</div>
				<label class="layui-form-label"></label>
				<span>
				<input type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="select" value="查询" />
				<input type="reset" class="layui-btn layui-btn-sm" value="重置">
				<input type="button" class="layui-btn layui-btn-sm" value="添加" onclick="openAdd()" />
				</span>
			</div>
			<input type="hidden" name="action" value="list" />
			<input type="hidden" name="pageIndex" value="1" />
			<input type="hidden" name="pageLimit" value="10" />
			</div>
			</form>
			
			<div class="layui-form">
        	<table class="layui-table">
            <colgroup>
                <col width="10%"><col width="15%"><col width="15%">
                <col width="15%"><col width="15%"><col width="15%"><col>
            </colgroup>
            <thead>
                <tr>
                    <th>序号</th><th>编号</th><th>名称</th>
                    <th>电话</th><th>操作</th>
                </tr>
            </thead>
            <tbody id="user_tbody"></tbody>
       	 	</table>
<!--         	<div id="pageInfoUser" style="text-align: right;padding-right: 30px"></div> -->
    		</div>
<!-- 			<div class="layui-form"> -->
<!-- 			<table class="layui-table" style="border: 1px solid #000000; width: 100%;"> -->
<!-- 				<thead> -->
<!-- 					<tr> -->
<!-- 						<td style="width: 15%;">ID</td> -->
<!-- 						<td style="width: 20%;">编号</td> -->
<!-- 						<td style="width: 20%;">姓名</td> -->
<!-- 						<td style="width: 20%;">电话</td> -->
<!-- 						<td style="width: 25%;">操作</td> -->
<!-- 					</tr> -->
<!-- 				</thead> -->
<!-- 				<tbody id="tbodyadd"></tbody> -->
<!-- 			</table> -->
<!-- 			</div> -->

		
		<script type="text/javascript">
			
			var layer = layui.layer;
			var $ = layui.jquery;
			var laytpl = layui.laytpl;
			var element = layui.element;
			var form = layui.form;
			var laydata = layui.laydata;
			var i = 1;
// 			var laytpl=layui.laytpl;
			element.render();
// 			$("#select").click(select);
// 			$("#add").click(add);
			
			form.on("submit(select)",function(data){
// 				console.log(data)
// 				return false;
				$.ajax({
					url:"<%=app %>/DepartmentServlet",
					data:data.field,
					dataType:'json',
					cache:false,
					type:'post',
					success:function(data){
// 						console.log(data);
						var html ="";
						$.each(data,function(i, dom) {
					        var code = dom.code?dom.code:'';
					        var name = dom.name?dom.name:'';
					        var del = dom.del?dom.del:'';
					        var d={i:i+1,name:name,code:code,del:del,count:dom.count};
// 					        html += getlaytpl('#tradd',d);
					        var string =  laytpl($("#tradd").html()).render(d);
					        
					        html += string;
						});
						$('#user_tbody').html(html);

						layer.msg("33");
					}
				});
			});
			
			refresh();
			function refresh(){
				$("input[type='reset']").click();
				$("input[value='查询']").click();
			}
			function openAdd(){
				openLayer('/web/page/addlist.jsp',refresh)
			}
			function openUpd(code){
				openLayer('/web/page/upd.jsp?code='+code,refresh)
			}
// 			function openUpd(code){
// 				 openLayer('/department?action=get&code='+code,refresh)
// 			}
			function openConfirm(func, title) {
				layer.confirm(title ? title : "确定进行该操作？", {
					icon : 3,
					title : '提示'
				}, func);
			}
			function del(code){
				openConfirm(function(index){
					$.ajax({
						url:"<%=app %>/DepartmentServlet",
						data:{action:'del',code:code},
						dataType:'text',
						cache:false,
						type:'post',
						success:function(data){
							if (data == 1) {
				                layer.msg('删除成功');
				                refresh();
				                $("input[name='pageIndex']").val(1);
				            } else if (data == "exist"){
				                layer.msg('删除失败--该数据已被使用');
				            } else {
				                layer.msg('删除失败');
				            }
						}
					});
				})
			}
			
			function openLayer(url,end){
				layer.open({
					type:2,
					area:['500px','460px'],
					fixed:false,
					maxmin:true,
					end:end,
					content:"<%=app %>"+url
				});
			}
// 			function add(){
// 				layer.open({
// 					type:1,
// 					title:"信息添加",
// 					closeBtn:2,
// 					area:['500px','460px'],
// 					shadeClose:true,
// 					content:$("#aa").html(),
// 					success:function(){
// 						$("#a1").click(testadd1)
// 						$("#a2").click(closeLayer)
// 					}
// 				})
// 			}
			function closeLayer(){
				layer.close(layer.index);
			}
			
		</script>
		
		
<!-- 		<td>{{ d.count }}</td> -->
		<script id="tradd" type="text/html">
			<tr>
			<td>{{ d.i }}</td><td>{{ d.code }}</td><td>{{ d.name }}</td>
			<td>{{ d.del }}</td>
			<td>
    		<input type='button' value='修改' class='layui-btn layui-btn-sm' 
           		onclick='openUpd("{{ d.code }}")'/>&nbsp;
    		<a href="javascript:del('{{ d.code }}')" 
       		class="layui-btn layui-btn-xs layui-btn-danger">
        		<i class="layui-icon layui-icon-delete"></i>
    		</a>&nbsp;
			</td>
			</tr>
		</script>
		
</body>
</html>